package com.situ.web.controller;

import com.situ.web.pojo.entity.Banji;
import com.situ.web.pojo.entity.Student;
import com.situ.web.pojo.vo.StudentBanjiVO;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.BanjiServiceImpl;
import com.situ.web.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 浏览器能访问JSP，也可以访问Servlet
 * 和Web开发相关的jar包不在jdk里面，在tomcat里面
 * http://localhost:8080/JavaWeb/hello.jsp
 * http://localhost:8080/JavaWeb/student
 */
//@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    //private IStudentDao studentDao = new StudentDaoImpl();
    private IStudentService studentService = new StudentServiceImpl();
    private IBanjiService banjiService = new BanjiServiceImpl();

    //访问Servlet时候默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post请求乱码问题
        req.setCharacterEncoding("UTF-8");
        /*HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect("/login.jsp");
            return;
        }*/

        //http://localhost:8080/student?method=selectAll
        //http://localhost:8080/student?method=deleteById&id=1
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectAll";
        }
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "toAdd":
                toAdd(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.toAdd");
        List<Banji> list = banjiService.selectAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("/student_add.jsp").forward(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender, null);
        studentService.update(student);

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/student?method=selectAll");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.toUpdate");
        String id = req.getParameter("id");
        Student student = studentService.selectById(Integer.parseInt(id));

        req.setAttribute("student", student);
        req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.add");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String banjiId = req.getParameter("banjiId");
        Student student = new Student();
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setGender(gender);
        student.setBanjiId(Integer.parseInt(banjiId));

        studentService.add(student);

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/student?method=selectAll");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.deleteById");
        String id = req.getParameter("id");
        studentService.deleteById(Integer.parseInt(id));

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/student?method=selectAll");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectAll");
        List<StudentBanjiVO> list = studentService.selectAll();
        //把list数据放到req里面
        req.setAttribute("list", list);
        //转发到student_list.jsp页面进行展示
        req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
    }
}
